package com.taozi.netty.socketio.use.classroomchat.service.socketio;

import com.corundumstudio.socketio.*;
import com.corundumstudio.socketio.listener.DataListener;
import com.taozi.netty.socketio.use.classroomchat.constant.EducationConstant;
import com.taozi.netty.socketio.use.classroomchat.entity.entity.education.ClassroomChat;
import com.taozi.netty.socketio.use.classroomchat.entity.entity.education.ClassroomUser;
import com.taozi.netty.socketio.use.classroomchat.mapper.education.ClassroomChatMapper;
import com.taozi.netty.socketio.use.classroomchat.mapper.education.ClassroomUserMapper;
import com.taozi.netty.socketio.use.classroomchat.entity.vo.education.ClassroomChatVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * description 课堂聊天消息监听
 *
 * @author mingtao
 * date：2019/7/1 16:33
 */
@Service("groupChatListener")
@Slf4j
public class GroupChatListener implements DataListener<ClassroomChatVo> {

    @Autowired
    ClassroomChatMapper classroomChatMapper;
    @Autowired
    ClassroomUserMapper classroomUserMapper;

    SocketIOServer socketIOServer;
    public void setServer(SocketIOServer server) {
        this.socketIOServer = server;
    }

    @Override
    public void onData(SocketIOClient socketIOClient, ClassroomChatVo classroomChatVo, AckRequest ackRequest) throws Exception {

        log.info("groupChat  " + classroomChatVo.toString());
        if(classroomChatVo == null || classroomChatVo.getClassroomId() == null || classroomChatVo.getUserId() == null){
            if(ackRequest.isAckRequested()){
                ackRequest.sendAckData("参数缺失");
            }
            return;
        }

        //查询人员是否登录或是否禁言
        classroomChatVo.setIsOnline(EducationConstant.STATUS_YES);
        ClassroomUser classroomUser = classroomUserMapper.selectByUserIdAndClassroomId(classroomChatVo);
        if(classroomUser == null){
            if(ackRequest.isAckRequested()){
                ackRequest.sendAckData("未登录");
            }
            return;
        }

        if(classroomUser.getIsBanChat() == EducationConstant.BAN_CHAT || classroomUser.getIsBanChat() == EducationConstant.BAN_ALL_CHAT){
            //被禁言
            if(ackRequest.isAckRequested()){
                ackRequest.sendAckData("已被禁言");
            }
            return;
        }

        //消息入库
        classroomChatMapper.insertSelective(convertClassroomChatVoToClassroomChat(classroomChatVo));

        //发送
        socketIOServer.getBroadcastOperations().sendEvent("room" + classroomChatVo.getClassroomId().toString(), classroomChatVo);


    }
    /**
    * @description 将 ClassroomChatVo 转为 ClassroomChat
    * @param classroomChatVo
    * @return ClassroomChat
    * @author mingtao
    * @date 15:52 2019/7/1
    **/
    private ClassroomChat convertClassroomChatVoToClassroomChat(ClassroomChatVo classroomChatVo){
        ClassroomChat classroomChat = new ClassroomChat();
        BeanUtils.copyProperties(classroomChatVo, classroomChat);
        classroomChat.setSendTime(new Date());
        return classroomChat;
    }
}
